﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;
using System.Collections.Specialized;   // string dictionary

/// <summary>
/// Widget 'WidgetRights' enables administrator to manage read/write acces of roles to different types of widgets
/// </summary>
public partial class WidgetRights : WidgetBase
{

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value>PrototypeWidget</value>
    public override string Name
    {
        get { return "WidgetRights"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); }
    }


    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
    }

    #region [ Settings ]

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return false; }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        this.EditPrototypeWidget.Visible = true;
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        this.EditPrototypeWidget.Visible = false;
    }
    #endregion

    #region [ DATA FILTER ]

    /// <summary>
    /// User filters view of widget rights
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void WidgetTypeFilterChanged(object sender, EventArgs e)
    {
        DisplayRights();
    }

    #endregion

    /// <summary>
    /// User clicked at one of the checkbox in Availability or Data Manipulation column
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void RightCheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        string[] parameters = checkBox.Text.Split('_');
        int rightID = Int32.Parse(parameters[0]);
        int roleID = Int32.Parse(parameters[1]);
        int widgetTypeID = Int32.Parse(parameters[2]);

        if (checkBox.ID == "CheckBoxWrite")
        {
            DAOWidgetRight.UpdateWriteRight(rightID, checkBox.Checked);
            ShowOperationResult(MessageType.Info, checkBox.Checked ? "WidgetRightsWriteRightEnabled" : "WidgetRightsWriteRightDisabled");
        }
        else //CheckBoxRead
        {
            if (checkBox.Checked)
            {
                DAOWidgetRight.AddViewRight(roleID, widgetTypeID);
                ShowOperationResult(MessageType.Info, "WidgetRightsWidgetEnabled");
            }
            else
            {
                DAOWidgetRight.RemoveViewRight(rightID);
                ShowOperationResult(MessageType.Info, "WidgetRightsWidgetDisabled");
            }
        }
        DisplayRights();
    }

    /// <summary>
    /// Displays the rights acording to data filter
    /// </summary>
    private void DisplayRights()
    {
        int roleID = Role.SelectedRole == null
            ? -1
            : Role.SelectedRole.ID;
        int widgetTypeID = WidgetType.SelectedWidgetTypeID;

        if (roleID != -1 || widgetTypeID != -1)
        {
            IList<WidgetRight> rights = DAOWidgetRight.GetWidgetRights(widgetTypeID, roleID);
            GridViewRights.DataSource = rights;
            GridViewRights.DataBind();
            //this.Message.Text = "";
        }
        else
        {
            // too many data to display
            //this.Message.Text = (string)GetLocalResourceObject("TooManyData");
        }
    }

    /// <summary>
    /// Handles the PreRender event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if ((this.GridViewRights.DataSource == null) || (this.GridViewRights.DataSource as IList<WidgetRight>).Count == 0)
        {
            this.ShowOperationResult(MessageType.Warning, IsPostBack ? "WidgetRightsTooManyData" : "WidgetRightsChoose");
        }
    }
}
